<?php
/**
 * ajax返回数据
 * @param  [string] $code    [状态码]
 * @param  [string] $message [提示消息]
 * @param  array  $data    [数据]
 * @return [json]
 */
function ajaxReturn($code,$message,$data = array()){
  header("Content-type:text/html;charset=utf8");
  $info = array(
    'code'    =>    $code,
    'message' =>    $message,
    'data'    =>    $data
  );
  exit(json_encode($info));
}
/**
 * 返回菜单分类
 * @param  [type] $cid [description]
 * @return [type]      [description]
 */
function getCidName($cid){
  if($cid == 0){
    $cidName = '前台菜单';
  }
  if($cid == 1){
    $cidName = '后台菜单';
  }
  return $cidName?$cidName:'未知错误';
}
/**
 * 获取菜单模板输出状态
 * @param  [type] $status [description]
 * @return [type]         [description]
 */
function getStatus($status){
  if($status == 0){
    $mes = '已停用';
  }
  if($status == 1){
    $mes = '已启用';
  }
  return $mes;
}
/**
 * 获取登录用户信息
 * @return [type] [description]
 */
function getLoginUserInfo(){
  return session('user')?session('user'):'';
}

/**
 * 获取加密后密码
 * @param  [string] $pwd [description]
 * @return [type]      [description]
 */
function getAdminPwd($pwd){
  return $pwd?md5($pwd.C('Admin_pwd_pro')):'';
}
/**
 * [getLevelName description]
 * @param  [type] $level [description]
 * @return [type]        [description]
 */
function getLevelName($level){
  if($level == 1){
    $levelName = '管理员';
  }
  if($level == 0){
    $levelName = '普通用户';
  }
  if($level == 2){
    $levelName = '超级管理员';
  }
  return $levelName?$levelName:'';
}
/**
 * [sendEmail description]
 * @param  [type] $to      [description]
 * @param  [type] $title   [description]
 * @param  [type] $content [description]
 * @return [type]          [description]
 */
function sendEmail($to, $title, $content) {
        vendor('PHPMailer.class#smtp');
        vendor('PHPMailer.class#phpmailer');
        $config = F('web_conf');
        $mail = new PHPMailer(); //实例化
        $mail->IsSMTP(); // 启用SMTP
        $mail->Host=C('MAIL_HOST'); //smtp服务器的名称（这里以QQ邮箱为例）
        $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
        $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
        $mail->Password = C('MAIL_PASSWORD') ; //邮箱密码
        $mail->From = C('MAIL_FROM'); //发件人地址（也就是你的邮箱地址）
        $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
        $mail->AddAddress($to,"尊敬的客户");
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
        $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
        $mail->Subject =$title; //邮件主题
        $mail->Body = $content; //邮件内容
        $mail->AltBody = "这是一个纯文本的身 体在非营利的HTML电子邮件客户端"; //
        //邮件正文不支持HTML的备用显示
        if(!$mail->send()) {
       return $mail->ErrorInfo;
    } else{
        return true;
    }
}
/**
 * [getRandStr description]
 * @param  [type] $length [description]
 * @return [type]         [description]
 */
function getRandStr($length){
  $str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456';
  for ($i=0; $i <$length ; $i++) {
    $res .=$str{mt_rand(0,strlen($str)-1)};
  }
  return $res;
}
/**
 * 获取网站配置，在系统配置处配置后才可以使用
 * @return [type] [description]
 */
function getWebConf(){
  return F('Web_conf')?F('Web_conf'):'';
}
